package thenewscats;

import java.text.DateFormat;
import java.util.Date;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.IdGeneratorStrategy;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(detachable = "true")
public class Article {
		
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private String title; // article title
	@Persistent
	private String author; // article author
	@Persistent
	private String category; // name of category this article belongs to
	@Persistent	
	private Date lastModify; // last modify date
	@Persistent	
	private String subtitle; // article subtitle	
	@Persistent
	private Text content; // main content of article
	@Persistent
	private BlobKey imageKey;
	
	
	public Article (String title, String author, String subtitle, 
			String category, String content){
		this.title = title;
		this.author = author;
		this.subtitle = subtitle;
		this.category = category;
		this.content = new Text(content);
		updateDate();
	}
	
	public Key getKey(){
		return key;
	}
	
	/* Returns the keystring from which you can identify the article */
	public String getKeystring(){
		return KeyFactory.keyToString(key);
	}
	
	public String getTitle(){
		return title;
	}
	
	public Date getLastModify(){
		return lastModify;
	}
	
	public String getAuthor(){
		return author;
	}
	
	public String getSubtitle(){
		return subtitle;
	}
	
	public String getCategory(){
		return category;
	}
	
	public String getContent(){
		return content.getValue();
	}
	
	public String getDateString(){
		DateFormat formatter = DateFormat.getDateTimeInstance();
		String datetime = formatter.format(lastModify);
				
		return datetime; 
	}
	
	public BlobKey getImageKey(){
		return imageKey;
	}
	
	/* Returns brief summary of the article, up to 250 chars*/
	public String getSummary(){
		String result;
		if (subtitle != null && subtitle.length() <= 0){
			result = this.getContent();	 
		}else{
			result = subtitle;
		}
		
		/* if description is too long, we'll chop it off */
		if (result.length() > 250){
			result = result.substring(0, 250);
			result = result + " ...";
		}		
		return result;
	}
	
	/*
	 * Returns the relative URL of the ArticleServlet for this
	 * article
	 */
	public String getArticleURL(){
		return "/article?action=view&articlekey=" + this.getKeystring();
	}
	
	/* Returns the relative URL to the ImageServlet which will
	 * serve the actual image
	 */
	public String getImageLocation(){
		
		// we don't have a picture
		if (imageKey == null){
			return "";
		} else {
			return "/image?blob-key=" + imageKey.getKeyString();
		}		
	}
	
	public void setTitle(String title){
		this.title = title;
		updateDate();
	}
	
	public void setAuthor(String author){
		this.author = author;
		updateDate();
	}
	
	public void setSubtitle(String subtitle){
		this.subtitle = subtitle;
		updateDate();
	}
	
	public void setCategory(String category){
		this.category = category;
		updateDate();
	}
	
	
	public void setContent(String content){
		this.content = new Text(content);
		updateDate();
	}
	
	public void setImageKey(BlobKey imgKey){
		imageKey = imgKey;
	}
		
	protected void updateDate(){
		lastModify = new Date();		
	}
}
